﻿package com.msjt.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.msjt.base.BaseAction;
import com.msjt.entity.Video;
import com.msjt.service.IVideoService;
import com.msjt.util.DeleteFile;
import com.msjt.util.MediocreExec;
import com.msjt.util.MyDate;
import com.msjt.util.PageMode;

/**
 * name:VideoAction function:视频模块Action author: kw time:2013.8.28 updateTime:
 */
@SuppressWarnings("serial")
public class VideoAction extends BaseAction {
	private Video video;
	@Resource
	private IVideoService videoService;
	// page Model
	private PageMode page;

	// 文件上传
	// 文件上传
	private List<File> file;
	private List<String> fileFileName;
	private List<String> fileContentType;

	public PageMode getPage() {
		return page;
	}

	public void setPage(PageMode page) {
		this.page = page;
	}

	public Video getVideo() {
		return video;
	}

	public void setVideo(Video video) {
		this.video = video;
	}

	public IVideoService getVideoService() {
		return videoService;
	}

	public void setVideoService(IVideoService videoService) {
		this.videoService = videoService;
	}

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	// 分页
	public void splitPage(PageMode page,String actionPath) {
		
		page.setAction(actionPath);
		page.setModelDrive("page");
		// 设置大小
		page.setSumRows(this.videoService.getVideoCount());
		page.createPageStr();
		// 得到结果集
		List<Video> videoList = this.videoService.getVideoPage(
				page.getPageIndex(), page.getPageSize());
		// System.out.println(videoList.get(0).getvDesc());
		// 得到分页字符串
		this.getRequest().put("videoList", videoList);

		this.getRequest().put("link", page.getPagingStr());
		 this.page=null;
	}

	// 后台查看视频列表，只加载ID，视频描述，日期，视频在点开后才加载
	public String showAdmin() throws Exception {
		if (this.page == null) {
			this.page = new PageMode();
			this.page.setPageIndex(1);
			this.page.setPageSize(10);
		}
		this.splitPage(this.page,"video_showAdmin.action");
		return "admin";
	}

	// 前台查看视频列表，只加载ID，视频描述，日期，视频在点开后才加载
	public String showMore() throws Exception {
		if (this.page == null) {
			this.page = new PageMode();
			this.page.setPageIndex(1);
			this.page.setPageSize(15);
		}
		this.splitPage(this.page,"video_showMore.action");
		return "showMore";
	}

	// 新上传视频
	// 此处做后期处理
	public String addVideo() throws Exception {
		try {
			
		
		// 上传视频
		// 上传视频begin
		// 上传文件
		// 得到工程保存视频的路径
		String realpath = ServletActionContext.getServletContext().getRealPath(
				"/uploadVideo/tempVideo");
		// 用时间来命名
		Date date = new Date();
		// 循环上传的文件
		String name = "";
		for (int i = 0; i < file.size(); i++) {
			// 存入文件名
			name = date.getTime() + (new Random()).nextInt(10000)
					+ fileFileName.get(i);
			InputStream is = new FileInputStream(file.get(i));
			// 得到视频保存的位置(根据realpath来得到视频保存的路径在tomcat下的该工程里)
			File destFile = new File(realpath, name);
			// 把图片写入到上面设置的路径里
			OutputStream os = new FileOutputStream(destFile);
			byte[] buffer = new byte[1000];
			int length = 0;
			while ((length = is.read(buffer)) > 0) {
				os.write(buffer, 0, length);
			}

			is.close();
			os.close();
		}
		// 项目路径
		String localpath = ServletActionContext.getServletContext()
				.getRealPath("");
		// 转码后存放路径
		String savepath = ServletActionContext.getServletContext().getRealPath(
				"/uploadVideo");
		// 视频转码
		MediocreExec med = new MediocreExec();
		// 工具路径
		String ffmpegPath = localpath + "\\tool\\ffmpeg.exe";
		// 用于指定要转换格式的文件,要截图的视频源文件
		String upFilePath = realpath + "\\" + name;
		// 格式转换后的的文件保存路径
		name = date.getTime() + (new Random()).nextInt(10000) + ".flv";
		String codcFilePath = savepath + "\\" + name;
		// 截图保存路径
		String mediaPicPath = savepath + "\\tempImage\\" + date.getTime()
				+ (new Random()).nextInt(10000) + ".jpg";
		med.executeCodecs(ffmpegPath, upFilePath, codcFilePath, mediaPicPath);
		String path = "uploadVideo/" + name;
		path = path.replace("\\", "/");
		if(this.video==null){
			this.video=new Video();
			this.video.setVideoDesc("暂无描述");
		}
		// 上传到数据库的路径
		this.video.setVideoPath(path);
		// 视频上传时间
		this.video.setVideoDate(new MyDate().getDateString());
		boolean flag = this.videoService.addVideo(this.video);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('上传失败！');history.go(-1);</script>");
			return "error";
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return showAdmin();
	}

	// 准备修改
	public String preUpdate() throws Exception {
		Video v = this.videoService.getVideoInfo(this.video);
		this.getRequest().put("video", v);
		return "update";
	}

	// 修改视频信息
	public String updateVideo() throws Exception {
		boolean flag = this.videoService.updateVideo(this.video);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('修改失败！');history.go(-1);</script>");
			return "error";
		}
		return showAdmin();
	}

	// 删除视频,删除视频需要删除服务器上的视频
	public String deleteVideo() throws Exception {
		System.gc();
		String path = ServletActionContext.getServletContext().getRealPath("");
		String videoPath = this.videoService.getVideoInfo(this.video)
				.getVideoPath();
		String realPath = path + "/" + videoPath;
		realPath = realPath.replace("\\", "/");
		
		DeleteFile d = new DeleteFile();
		d.deleteFolder(realPath);
		//删除临时文件目录
		d.delAllFile(path+"\\uploadVideo\\tempImage\\");
		d.delAllFile(path+"\\uploadVideo\\tempVideo\\");
		boolean flag = this.videoService.deleteVideo(this.video);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('删除失败！');history.go(-1);</script>");
			return "error";
		}
		return this.showAdmin();
	}

	// 后台查看详细
	public String getVideoDetail() throws Exception {
		Video v = this.videoService.getVideoInfo(this.video);
		this.getRequest().put("video", v);
		return "detail";
	}

	// 根据ID得到视频的详细信息
	public String getVideoInfo() throws Exception {
		Video v = this.videoService.getVideoInfo(this.video);
		this.getRequest().put("video", v);
		return "showDetail";
	}

	// 批量删除
	public String deleteMore() throws Exception {
		
		String idString = ServletActionContext.getRequest().getParameter(
				"idString");
		idString = idString.substring(0, idString.length() - 1);
		
		List<String> videoList=this.videoService.getVideoPath(idString);
		//删除本地文件
		DeleteFile d = new DeleteFile();
		if(videoList!=null){
			for (int i = 0; i < videoList.size(); i++) {
				System.gc();
				String path = ServletActionContext.getServletContext().getRealPath("");
				String videoPath = videoList.get(i);
				String realPath = path + "/" + videoPath;
				realPath = realPath.replace("\\", "/");
				d.deleteFolder(realPath);
				//删除临时文件目录
				d.delAllFile(path+"\\uploadVideo\\tempImage\\");
				d.delAllFile(path+"\\uploadVideo\\tempVideo\\");
			}
		}
		
		
		boolean flag = this.videoService.deleteMore(idString);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('删除失败！');history.go(-1);</script>");
			return "error";
		}
		return showAdmin();
	}
}
